package com.hfut.jpoa.service.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.util.DateUtils;
import com.hfut.jpoa.dao.CustomerDAO;
import com.hfut.jpoa.dao.DispatchDAO;
import com.hfut.jpoa.dto.DispatchDTO;
import com.hfut.jpoa.model.Customer;
import com.hfut.jpoa.model.Dispatch;
import com.hfut.jpoa.model.User;
import com.hfut.jpoa.service.DispatchService;
import com.hfut.jpoa.service.exception.DispatchException;
import com.hfut.jpoa.service.exception.SysparaException;
import com.hfut.jpoa.service.exception.UserException;
import com.sun.org.apache.regexp.internal.recompile;

public class DispatchServiceImpl implements DispatchService
{

	private static final Logger LOGGER = LoggerFactory
	        .getLogger(DispatchServiceImpl.class);

	/**
	 * 调度DAO
	 */
	private DispatchDAO dispatchDAO;

	/**
	 * 客户DAO
	 */
	private CustomerDAO customerDAO;

	public ResultDto<Dispatch> queryInfoForPage(DispatchDTO condition)
	        throws DispatchException
	{
		ResultDto<Dispatch> findInfoForPage = dispatchDAO
		        .findInfoForPage(condition);
		return findInfoForPage;
	}

	public void insert(Dispatch condition) throws DispatchException
	{
		// TODO Auto-generated method stub

	}

	public void update(Dispatch record) throws DispatchException
	{
		dispatchDAO.updateByPrimaryKey(record);

	}

	public Dispatch findInfo(Dispatch condition) throws DispatchException
	{

		return dispatchDAO.findInfoForPage(condition);
	}

	public boolean codeValidator(Dispatch condition) throws DispatchException
	{
		// TODO Auto-generated method stub
		return false;
	}

	public List<Dispatch> queryList(Dispatch condition)
	        throws DispatchException
	{
		// TODO Auto-generated method stub
		return null;
	}

	public void setDispatchDAO(DispatchDAO dispatchDAO)
	{
		this.dispatchDAO = dispatchDAO;
	}

	public void saveDispatch(Dispatch dispatch) throws DispatchException
	{
		if (null == dispatch)
		{
			LOGGER.error("dispatch is empty.");
			throw new DispatchException("dispatch info is empty");
		}
		// 如果不存在客户编码
		if (StringUtils.isBlank(dispatch.getCustomerId()))
		{

			Customer record = new Customer();
			record.setName(StringUtils.defaultString(dispatch.getCustomerName()));
			record.setLinkman(StringUtils.defaultString(dispatch
			        .getRepairpeopleName()));
			record.setContactPhone(StringUtils.defaultString(dispatch
			        .getRepairPeoplePhone()));
			record.setNote("报修新增:" + dispatch.getName());
			Date createDate = null;
			try
			{
				createDate = DateUtils.getNowDate();
			} catch (ParseException e)
			{
				LOGGER.error("时间格式化错误.");
				throw new DispatchException("时间格式化错误.");
			}
			record.setCreateDate(createDate);
			record.setLastUpdateDate(createDate);
			record.setPathId(dispatch.getPathId());
			Integer insertSelective = customerDAO.insertSelective(record);
			if (null == insertSelective)
			{
				LOGGER.error("新增客户信息异常.");
				throw new DispatchException("新增客户信息异常.");
			}
			dispatch.setCustomerId(insertSelective + "");
		}
		// 新增一条客户信息
		dispatchDAO.insert(dispatch);
	}

	public void packageUser(List<Dispatch> result, List<User> lstUsers)
	        throws DispatchException
	{
		for (int i = 0; i < result.size(); i++)
		{
			String customerId = result.get(i).getMaintenanceManId();
			List<String> lstCustomerName = new ArrayList<String>();
			if (StringUtils.isNotBlank(customerId))
			{
				String[] customerIds = customerId.split(",");
				for (int j = 0; j < customerIds.length; j++)
				{
					for (int k = 0; k < lstUsers.size(); k++)
					{
						if (StringUtils.equals(lstUsers.get(k).getUserCode(),
						        customerIds[j]))
						{
							lstCustomerName.add(lstUsers.get(k).getName());
						}
					}
				}
			}
			result.get(i).setMaintenanceManName(
			        StringUtils.join(lstCustomerName, ","));
		}

	}

	public CustomerDAO getCustomerDAO()
	{
		return customerDAO;
	}

	public void setCustomerDAO(CustomerDAO customerDAO)
	{
		this.customerDAO = customerDAO;
	}
}
